import { Resolver, Query, Args, Mutation } from '@nestjs/graphql';
import { UserService } from '../user/user.service';
import { AdminService } from '../admin/admin.service';
import { AuthService } from './auth.service';
import { UnauthorizedException } from '@nestjs/common';

@Resolver()
export class AuthResolver {
  constructor(private readonly userService: UserService,
              private readonly adminService: AdminService,
              private readonly authService: AuthService) {}

  @Mutation('login')
  async login(@Args() args: { username: string, password: string }) {
    const result = await this.authService.validateAdmin(args.username, args.password);
    if (!result) {
      throw new UnauthorizedException();
    }
    return await this.authService.login(result);
  }

  @Mutation('changeAdminPassword')
  async changeAdminPassword(@Args() args: { username: string, password: string, confirmPassword: string }) {
    // const result = await this.authService.validateUser(args.username, args.password);
    // if (!result) {
    //   throw new BadRequestException();
    // }
    return await this.adminService.changePassword(args.username, args.password);
  }

  @Mutation('changeUserPassword')
  async changeUserPassword(@Args() args: { username: string, password: string, confirmPassword: string }) {
    // const result = await this.authService.validateUser(args.username, args.password);
    // if (!result) {
    //   throw new BadRequestException();
    // }
    return await this.userService.changePassword(args.username, args.password);
  }
}